home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Library / Manuels & Misc / Assembly / AOA.ZIP / CH15 / FCMP.ASM < prev    next >
Encoding:
Assembly Source File  |  1996-03-25  |  4.1 KB  |  221 lines

  1. ; FCMP.ASM-    A file comparison program that demonstrates the use
  2. ;        of the 80x86 string instructions.
  3.  
  4.         .xlist
  5.         include     stdlib.a
  6.         includelib    stdlib.lib
  7.         .list
  8.  
  9. dseg        segment    para public 'data'
  10.  
  11. Name1        dword    ?        ;Ptr to filename #1
  12. Name2        dword    ?        ;Ptr to filename #2
  13. Handle1        word    ?        ;File handle for file #1
  14. Handle2        word    ?        ;File handle for file #2
  15. LineCnt        word    0        ;# of lines in the file.
  16.  
  17. Buffer1        db    256 dup (0)    ;Block of data from file 1
  18. Buffer2        db    256 dup (0)    ;Block of data from file 2
  19.  
  20. dseg        ends
  21.  
  22. wp        equ    <word ptr>
  23.  
  24.  
  25. cseg        segment    para public 'code'
  26.         assume    cs:cseg, ds:dseg
  27.  
  28.  
  29. ; Error- Prints a DOS error message depending upon the error type.
  30.  
  31. Error        proc    near
  32.         cmp    ax, 2
  33.         jne    NotFNF
  34.         print
  35.         db    "File not found",0
  36.         jmp    ErrorDone
  37.  
  38. NotFNF:        cmp    ax, 4
  39.         jne    NotTMF
  40.         print
  41.         db    "Too many open files",0
  42.         jmp    ErrorDone
  43.  
  44. NotTMF:        cmp    ax, 5
  45.         jne    NotAD
  46.         print
  47.         db    "Access denied",0
  48.         jmp    ErrorDone
  49.  
  50. NotAD:        cmp    ax, 12
  51.         jne    NotIA
  52.         print
  53.         db    "Invalid access",0
  54.         jmp    ErrorDone
  55.  
  56. NotIA:
  57. ErrorDone:    putcr
  58.         ret
  59. Error        endp
  60.  
  61.  
  62.  
  63.  
  64. ; Okay, here's the main program.  It opens two files, compares them, and
  65. ; complains if they're different.
  66.  
  67. Main        proc
  68.         mov    ax, seg dseg        ;Set up the segment registers
  69.         mov    ds, ax
  70.         mov    es, ax
  71.  
  72.         meminit
  73.  
  74. ; File comparison routine.  First, open the two source files.
  75.  
  76.         argc
  77.         cmp    cx, 2        ;Do we have two filenames?
  78.         je    GotTwoNames
  79.         print
  80.         db    "Usage: fcmp file1 file2",cr,lf,0
  81.         jmp    Quit
  82.  
  83. GotTwoNames:    mov    ax, 1        ;Get first file name
  84.         argv
  85.         mov    wp Name1, di
  86.         mov    wp Name1+2, es
  87.  
  88. ; Open the files by calling DOS.
  89.  
  90.         mov    ax, 3d00h    ;Open for reading
  91.         lds    dx, Name1
  92.         int    21h
  93.         jnc    GoodOpen1
  94.         printf
  95.         db    "Error opening %^s:",0
  96.         dd    Name1
  97.         call    Error
  98.         jmp    Quit
  99.  
  100. GoodOpen1:    mov    dx, dseg
  101.         mov    ds, dx
  102.         mov    Handle1, ax
  103.  
  104.         mov    ax, 2        ;Get second file name
  105.         argv
  106.         mov    wp Name2, di
  107.         mov    wp Name2+2, es
  108.  
  109.         mov    ax, 3d00h    ;Open for reading
  110.         lds    dx, Name2
  111.         int    21h
  112.         jnc    GoodOpen2
  113.         printf
  114.         db    "Error opening %^s:",0
  115.         dd    Name2
  116.         call    Error
  117.         jmp    Quit
  118.  
  119. GoodOpen2:    mov    dx, dseg
  120.         mov    ds, dx
  121.         mov    Handle2, ax
  122.  
  123. ; Read the data from the files using blocked I/O
  124. ; and compare it.
  125.  
  126.         mov    LineCnt, 1
  127. CmpLoop:    mov    bx, Handle1    ;Read 256 bytes from
  128.         mov    cx, 256        ; the first file into
  129.         lea    dx, Buffer1    ; Buffer1.
  130.         mov    ah, 3fh
  131.         int    21h
  132.         jc    FileError
  133.         cmp    ax, 256        ;Leave if at EOF.
  134.         jne    EndOfFile
  135.  
  136.         mov    bx, Handle2    ;Read 256 bytes from
  137.         mov    cx, 256        ; the second file into
  138.         lea    dx, Buffer2    ; Buffer2
  139.         mov    ah, 3fh
  140.         int    21h
  141.         jc    FileError
  142.         cmp    ax, 256        ;If we didn't read 256 bytes,
  143.         jne    BadLen        ; the files are different.
  144.  
  145. ; Okay, we've just read 256 bytes from each file, compare the buffers
  146. ; to see if the data is the same in both files.
  147.  
  148.         mov    ax, dseg
  149.         mov    ds, ax
  150.         mov    es, ax
  151.         mov    cx, 256
  152.         lea    di, Buffer1
  153.         lea    si, Buffer2
  154.         cld
  155.     repe    cmpsb
  156.         jne    BadCmp
  157.         jmp    CmpLoop
  158.  
  159.  
  160. FileError:    print
  161.         db    "Error reading files: ",0
  162.         call    Error
  163.         jmp    Quit
  164.  
  165.  
  166. BadLen:        print
  167.         db    "File lengths were different",cr,lf,0
  168.  
  169. BadCmp:        print
  170.         db      7,"Files were not equal",cr,lf,0
  171.  
  172.         mov    ax, 4c01h        ;Exit with error.
  173.         int    21h
  174.  
  175.  
  176. ; If we reach the end of the first file, compare any remaining bytes
  177. ; in that first file against the remaining bytes in the second file.
  178.  
  179. EndOfFile:    push    ax            ;Save final length.
  180.         mov    bx, Handle2
  181.         mov    cx, 256
  182.         lea    dx, Buffer2
  183.         mov    ah, 3fh
  184.         int    21h
  185.         jc    BadCmp
  186.  
  187.         pop    bx            ;Retrieve file1's length.
  188.         cmp    ax, bx            ;See if file2 matches it.
  189.         jne    BadLen
  190.  
  191.         mov    cx, ax            ;Compare the remaining
  192.         mov    ax, dseg        ; bytes down here.
  193.         mov    ds, ax
  194.         mov    es, ax
  195.         lea    di, Buffer2
  196.         lea    si, Buffer1
  197.     repe    cmpsb
  198.         jne    BadCmp
  199.  
  200. Quit:        mov    ax, 4c00h        ;Set Exit code to okay.
  201.         int    21h
  202. Main        endp
  203.  
  204. cseg            ends
  205.  
  206.  
  207.  
  208. ; Allocate a reasonable amount of space for the stack (2k).
  209.  
  210. sseg        segment    para stack 'stack'
  211. stk        db    256 dup ("stack   ")
  212. sseg        ends
  213.  
  214.  
  215. ; zzzzzzseg must be the last segment that gets loaded into memory!
  216.  
  217. zzzzzzseg    segment    para public 'zzzzzz'
  218. LastBytes    db    16 dup (?)
  219. zzzzzzseg    ends
  220.         end    Main
  221.